<?
	class Paging
	{
		// Propiedades del Paginado
		var $nFrom;
		var $nTo;
		var $nqItemsPage;
		var $nqTotalItems;
		var $nqLimitItems;
		//Propiedades del Orden
		var $cOrderField;
		var $cAscending;
		// Propiedades comunes
		var $cUrlBase;
		var $cClassContainer;
		var $bMergeParameter = true;
		var $cTarget = 'body_main';
		
	function setMergeParameter( $bOn = true)
	{
		$this->bMergeParameter = $bOn;
	}

		function setClassContainer( $cClassContainer)
		{
			$this->cClassContainer = strtolower($cClassContainer);
		}
		function setUrlBase( $cUrlBase)
		{
			$this->cUrlBase = $cUrlBase;
		}
		function setNoPaging()
		{
			$this->nFrom = SIN_PAGINADO;
		}
		function setPage( $nFrom, $nTo )
		{
			$this->nFrom = $nFrom;
			$this->nTo = $nTo;
		}
		function Paging ( $cUrlBase = '', $cClassContainer = '', $nqItemsPage = FILAS_PAGINADO, $nqLimitItems = LIMITE_SIN_PAGINADO )
		{
			// Inicializa las Propiedades del Orden
			$cOrderBy	= getParam( PARAM_ORDERBY );
			list( $this->cOrderField, $this->cAscending ) = explode( '|' , $cOrderBy);
			// Inicializa las Propiedades del Paginado
			$this->nqItemsPage = $nqItemsPage;
			$this->nqLimitItems = $nqLimitItems;
			$this->nFrom = getParam( PARAM_FROM, '0' );
			$this->nTo	= $this->nFrom + $this->nqItemsPage;
			$this->nqTotalItems = '';
			$this->cUrlBase = $cUrlBase;
			// Arma una cadena con la URL Base para hacer el resto de los links
			if ( MODO_AJAX == true	)
			{
				$this->cClassContainer = $cClassContainer;
				//$this->cUrlBase = replaceString( PARAM_REFRESH, "class", $this->cUrlBase);
				$this->cUrlBase = replaceString( PARAM_CLASS, $cClassContainer, $this->cUrlBase);
			}
			$this->cUrlBase = replaceString( PARAM_FROM, '', $this->cUrlBase);
			$this->cUrlBase = replaceString( PARAM_ORDERBY, '', $this->cUrlBase);
		}
		function setTotalItems ( $nqTotalItems )
		{
			$this->nqTotalItems = $nqTotalItems;
			$this->nTo	= min( $this->nFrom + $this->nqItemsPage, $this->nqTotalItems	);

			/* Debug de Uso Alto del Servidor
			if ( $this->nqTotalItems > 100 && $this->nFrom == SIN_PAGINADO )
			{
				addMessage( 'Por favor evite el alto uso del Servidor, usando filtros' );
			}
			*/
		}
		//
		function getFrom()
		{
			return $this->nFrom;
		}
		function getTo()
		{
			return $this->nTo;
		}
		//
		function getLength()
		{
			return $this->nqItemsPage;
		}
		//
		function makePages()
		{
			$cReturn = "";
			if ( !$this->nqTotalItems ||  $this->nqTotalItems  < $this->nqItemsPage) return false;
			$nTotalPage = ceil($this->nqTotalItems / $this->nqItemsPage);
			$nActualPage = ceil($this->nFrom / $this->nqItemsPage) + 1;
			$nFromPage = max ( $nActualPage - 10, 1 );
			$nToPage = min ( $nActualPage + 10 , $nTotalPage );
			// Manda anterior
			if ( $nFromPage > 1 )
			{
				$nFrom	 = ($nActualPage-2) * $this->nqItemsPage;
				$cUrl = $this->cUrlBase . '&amp;' . PARAM_ORDERBY . '=' . $this->cOrderField . '|' . $this->cAscending . '&amp;' . PARAM_FROM . '='  . $nFrom ;
				if ( MODO_AJAX == true	) 
				{
					$cMergeFunction = ( $this->bMergeParameter ) ? "mergeParameterUrl( \"" . $this->cTarget . "\" , \"". PARAM_FROM . '='  . $nFrom	 . "\")'; " :"";
					$cUrl = "javascript:void(0);' onclick='javascript: send(\"$cUrl\",refreshElement, \"" . $this->cClassContainer . "\" ); $cMergeFunction return false;";
				}
				$cReturn .= "<a class='paging_text' href='$cUrl' title='Ir a pagina " . ($nActualPage -1 ). "'><<&nbsp;</a>";
			}
			if ($nTotalPage > 1 )
			{
				for( $nPage = $nFromPage; $nPage <= $nToPage ; $nPage++ )
				{
					$nFrom	 = ($nPage -1 ) * $this->nqItemsPage;
					$cClass = ($nPage == $nActualPage ) ? "paging_select": "paging_num";
					$cUrl = $this->cUrlBase . '&amp;' . PARAM_ORDERBY . '=' . $this->cOrderField . '|' . $this->cAscending . '&amp;' . PARAM_FROM . '='  . $nFrom	 ;
					if ( MODO_AJAX == true	) 
					{
						$cMergeFunction = ( $this->bMergeParameter ) ? "mergeParameterUrl( \"" . $this->cTarget . "\" , \"". PARAM_FROM . '='  . $nFrom	 . "\")'; " :"";
						$cUrl = "javascript:void(0);' onclick='javascript: send(\"$cUrl\",refreshElement, \"" . $this->cClassContainer . "\" ) ; $cMergeFunction return false;";
					}
					$cReturn .= "<a class='$cClass' href='$cUrl' title='Ir a pagina $nPage'>$nPage&nbsp;</a>";
				}
			}
			// Manda siguiente
			if ( $nToPage < $nTotalPage  )
			{
				$nFrom	 = ($nActualPage) * $this->nqItemsPage;
				$cUrl = $this->cUrlBase . '&amp;' . PARAM_ORDERBY . '=' . $this->cOrderField . '|' . $this->cAscending . '&amp;' . PARAM_FROM . '='  . $nFrom		 ;
				if ( MODO_AJAX == true	) 
				{
					$cMergeFunction = ( $this->bMergeParameter ) ? "mergeParameterUrl( \"" . $this->cTarget . "\" , \"". PARAM_FROM . '='  . $nFrom	 . "\")'; " :"";
					$cUrl = "javascript:void(0);' onclick='javascript: send(\"$cUrl \",refreshElement, \"" . $this->cClassContainer . "\" ); $cMergeFunction return false;";
				}
				$cReturn .= "<a class='paging_text' href='$cUrl' title='Ir a pagina " . ($nActualPage +1 ). "'>>></a>";
			}
			return $cReturn;
		}
		function makeButtons()
		{
			$cReturn = "";
			// Solo si tiene Paginado
			if ( $this->nFrom == SIN_PAGINADO )
			{
				return false;
			}
			$cReturn			= "";
			// Arma el Link del Orden para que continue con el paginado
			if ( $this->cOrderField )
			{
				$cUrlButton = $this->cUrlBase . '&amp;' . PARAM_ORDERBY . '=' . $this->cOrderField . '|' . $this->cAscending . '&amp;' . PARAM_FROM . '=';
			} else
			{
				$cUrlButton = $this->cUrlBase . '&amp;' . PARAM_FROM . '=';
			}
			// Variables para armar los links
			$nAnterior		= ($this->nFrom - $this->nqItemsPage );
			$nSiguiente	 = ($this->nFrom + $this->nqItemsPage );
			$cInicial		 = $cUrlButton	. '0';
			$cAnterior		= $cUrlButton . $nAnterior ;
			$cSiguiente	 = $cUrlButton . $nSiguiente;
			$cSinPaginado = $cUrlButton . SIN_PAGINADO;

			// Si tiene Total de Filas
			if ( $this->nqTotalItems )
			{
				$nFinal = $this->nqItemsPage * (int)( ($this->nqTotalItems-1) / $this->nqItemsPage ) ;
				$cFinal = $cUrlButton . $nFinal;
			}
			if ( MODO_AJAX == true	)
			{
				$cInicial = "javascript:void(0);' onclick='Javascript: send(\"$cInicial\",refreshElement, \"" . $this->cClassContainer . "\" );";
				$cInicial .=  ( $this->bMergeParameter ) ? "mergeParameterUrl( \"" . $this->cTarget . "\" , \"". PARAM_FROM . '='  . 0 . "\")'; return false;" :"return false;";
				
				$cAnterior = "javascript:void(0);' onclick='Javascript: send(\"$cAnterior\",refreshElement, \"" . $this->cClassContainer . "\" );";
				$cAnterior .=  ( $this->bMergeParameter ) ? "mergeParameterUrl( \"" . $this->cTarget . "\" , \"". PARAM_FROM . '='  . $nSiguiente. "\")'; return false;" :"return false;";
				
				$cSiguiente = "javascript:void(0);' onclick='Javascript:  send(\"$cSiguiente\",refreshElement, \"" . $this->cClassContainer . "\" );";
				$cSiguiente .=  ( $this->bMergeParameter ) ? " mergeParameterUrl( \"" . $this->cTarget . "\" , \"". PARAM_FROM . '='  . $nSiguiente. "\")'; return false;" :" return false;";
				
				$cFinal = "javascript:void(0);' onclick='Javascript: send(\"$cFinal\",refreshElement, \"" . $this->cClassContainer . "\" );";
				$cFinal .= ( $this->bMergeParameter ) ? "mergeParameterUrl( \"" . $this->cTarget . "\" , \"". PARAM_FROM . '='  . $nFinal. "\")'; return false;" :"return false;";
				
				$cSinPaginado = "javascript:void(0);' onclick='Javascript: return send(\"$cSinPaginado\",refreshElement, \"" . $this->cClassContainer . "\" )";
			}
			// Si no es la primer pagina
			if ( $nAnterior >= 0 )
			{
				$cReturn .= "<a accesskey='/' class='paging_button' href='$cInicial' title='Primera Pagina'>&nbsp;" . SIMBOLO_PRIMERO . "&nbsp;</a>";
				$cReturn .= "<a accesskey='-' class='paging_button' href='$cAnterior' title='Pagina anterior'>&nbsp;" . SIMBOLO_ANTERIOR . "&nbsp;</a>";
			} else
			{
				$cReturn .= "<span class='paging_button' >&nbsp;" . SIMBOLO_PRIMERO . "&nbsp;</span><span class='paging_button' >&nbsp;" . SIMBOLO_ANTERIOR . "&nbsp;</span>";
			}

			// Si hay mas paginas
			if ( ($this->nqTotalItems > $this->nTo && $this->nqTotalItems < $this->nqLimitItems) || $this->nqLimitItems == 0 )
			{
				$cReturn .= "<a class='paging_button' href='$cSinPaginado' title='Sin Paginado'>&nbsp;" . SIMBOLO_SIN_PAGINADO . "&nbsp;</a>";
			} else
			{
				$cReturn .= "<span class='paging_button' >&nbsp;" . SIMBOLO_SIN_PAGINADO . "&nbsp;</span>";
			}

			// Si no es la ultima pagina
			if ( $nSiguiente < $this->nqTotalItems || $this->nqTotalItems == '' )
			{
				$cReturn .= "<a accesskey='+' class='paging_button' href='$cSiguiente' title='Siguiente pagina'" . SIMBOLO_SIGUIENTE . "&nbsp;>&nbsp;</a>";
				if ( $this->nqTotalItems )
				{
					$cReturn .= "<a accesskey='*' class='paging_button' href='$cFinal' title='Ultima Pagina'>&nbsp;" . SIMBOLO_ULTIMO . "&nbsp;</a>";
				} else
				{
					$cReturn .= "<span class='paging_button' >&nbsp;" . SIMBOLO_ULTIMO . "&nbsp;</span>";
				}
			} else
			{
				$cReturn .= "<span class='paging_button' >&nbsp;" . SIMBOLO_SIGUIENTE . "&nbsp;</span>";
				$cReturn .= "<span class='paging_button' >&nbsp;" . SIMBOLO_ULTIMO . "&nbsp;</span>";
			}
			return $cReturn;
		}
		//
		function makeReferences()
		{
			$cReturn = "";
			// Si no hay Paginado
			if ( $this->nFrom == SIN_PAGINADO )
			{
				$cReturn = $this->nqTotalItems;
			} else
			{
				$cReturn = ($this->nFrom + 1) . "-" . $this->nTo;
				if ( $this->nqTotalItems )
				{
					$cReturn	.=	"/" . $this->nqTotalItems;
				}
			}
			$cReturn = "<span class='paging_reference' >" . $cReturn . "</span>";
			return $cReturn;
		}
		function setDefaultOrderBy( $cOrderField, $bAscending )
		{
			if ( $this->cOrderField	== '' )
			{
				$this->cOrderField	= $cOrderField;
				$this->cAscending	 = $bAscending == true ? 'ASC' : 'DESC';
			}
		}
		function setOrderBy( $cOrderField, $bAscending )
		{
			$this->cOrderField	= $cOrderField;
			$this->cAscending	 = $bAscending == true ? 'ASC' : 'DESC';
		}
		//
		function getOrderBy()
		{
			if ( $this->cOrderField )
			{
				return ' ORDER BY ' . $this->cOrderField . ' '	. $this->cAscending;
			} else
			{
				return "";
			}
		}
		function makeMultiOrderBy( $cOrderField )
		{
			$cAscending = 'asc';
			// Si ahora esta ordenado por el campo => invierte el orden
			$cClass1 =  ( trim($cOrderField) == $this->cOrderField   &&  $this->cAscending == 'asc' )  ? 'paging_select_orderby': 'paging_orderby';
			$cOrderLink1 = $this->cUrlBase . '&amp;' . PARAM_FROM . '=0&amp;' . PARAM_ORDERBY . "=$cOrderField|asc";

			$cClass =  ( trim($cOrderField) == $this->cOrderField   &&  $this->cAscending == 'desc' )  ? 'paging_select_orderby': 'paging_orderby';
			$cOrderLink = $this->cUrlBase . '&amp;' . PARAM_FROM . '=0&amp;' . PARAM_ORDERBY . "=$cOrderField|desc";
			if ( MODO_AJAX == true )
			{
				$cOrderLink1 = "javascript:void(0);' onclick='Javascript: return send(\"$cOrderLink\",refreshElement, \"" . $this->cClassContainer . "\" )";
				$cOrderLink2 = "javascript:void(0);' onclick='Javascript: return send(\"$cOrderLink\",refreshElement, \"" . $this->cClassContainer . "\" )";
			}
			$cReturn = "<a class='$cClass1' href='$cOrderLink1' title='Ordena Ascendentemente'>&nbsp;" . SIMBOLO_ASCENDENTE. "</a>";
			$cReturn .= "<a class='$cClass2' href='$cOrderLink2' title='Ordena Descendentemente'>&nbsp; " . SIMBOLO_DESCENDENTE . "</a>";

			return $cReturn;
		}

		function makeOrderBy( $cOrderField , $cTexto = '')
		{
			$cAscending = 'asc';
			$cClass = 'paging_orderby';
			// Si ahora esta ordenado por el campo => invierte el orden
			if ( trim($cOrderField) == $this->cOrderField  )
			{
				if ( $this->cAscending == 'asc' ) $cAscending = 'desc';
				$cClass = 'paging_select_orderby';
			}
			$cOrderLink = $this->cUrlBase . '&amp;' . PARAM_FROM . '=0&amp;' . PARAM_ORDERBY . "=$cOrderField|$cAscending";
			if ( MODO_AJAX == true )
			{
				$cOrderLink = "javascript:void(0);' onclick='Javascript: return send(\"$cOrderLink\",refreshElement, \"" . $this->cClassContainer . "\" )";
			}
			if ( $cAscending == 'asc' )
			{
				if ( $cTexto =='' ) $cTexto  = "&nbsp;" . SIMBOLO_ASCENDENTE. "&nbsp";
				$cReturn = "<a class='$cClass' href='$cOrderLink' title='Ordena Ascendentemente'>$cTexto</a>";
			} else
			{
				if ( $cTexto =='' ) $cTexto  = "&nbsp; " . SIMBOLO_DESCENDENTE . "&nbsp;";
				$cReturn = "<a class='$cClass' href='$cOrderLink' title='Ordena Descendentemente'>$cTexto</a>";
			}
			return $cReturn;
		}
	}
?>